package com.shell.governor.security.user.service.impl;

import java.util.Date;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.shell.bee.base.utils.DateUtil;
import com.shell.bee.base.utils.StringUtil;
import com.shell.bee.base.utils.SystemUtil;
import com.shell.bee.entity.mvc.RetMsg;
import com.shell.bee.persistence.service.impl.PersistentServiceImpl;
import com.shell.governor.modules.po.PubDictionary;
import com.shell.governor.modules.service.PubDictionaryService;
import com.shell.governor.security.user.dao.PubSecurityEditLogDao;
import com.shell.governor.security.user.dao.PubSecurityRuleDao;
import com.shell.governor.security.user.po.PubSecurityEditLog;
import com.shell.governor.security.user.po.PubSecurityRule;
import com.shell.governor.security.user.service.PubSecurityService;
import com.shell.governor.security.user.vo.RuleCheckMsg;
import com.shell.governor.user.po.PubUser;
import com.shell.governor.user.service.PubUserService;

@Service
public class PubSecurityServiceImpl extends PersistentServiceImpl<PubSecurityRule> implements PubSecurityService {
	@Autowired
	private PubSecurityRuleDao securityRuleDao;
	@Autowired
	private PubSecurityEditLogDao securityEditLogDao;
	@Autowired
	private PubDictionaryService pubDictionaryService;
	@Autowired
	private PubUserService pubUserService;

	public List<PubSecurityRule> selectSecurityRules() {
		return this.securityRuleDao.selectAll();
	}

	public RetMsg updateSecurityRule(PubSecurityRule PubSecurityRule) {
		int retflag = 1;
		retflag = this.securityRuleDao.updateByPrimaryKey(PubSecurityRule);
		if (retflag == 1) {
			return new RetMsg("success", "��������");
		}
		return new RetMsg("error", "��������");
	}

	public RuleCheckMsg checkSecurityRule(String userId, String password, String securityType, boolean checkExpire) {
		RuleCheckMsg checkMsg = new RuleCheckMsg();
		checkMsg.setType(securityType);
		PubSecurityRule PubSecurityRule = new PubSecurityRule();
		PubSecurityRule.setSecurityType(securityType);
		PubSecurityRule = (PubSecurityRule) this.securityRuleDao.selectOne(PubSecurityRule);
		PubSecurityEditLog log = new PubSecurityEditLog();
		log.setSecurityType(securityType);
		log.setUserId(userId);
		log = (PubSecurityEditLog) this.securityEditLogDao.selectOne(log);
		if (PubSecurityRule != null) {
			String createDateStr = "";

			String ruleStrength = PubSecurityRule.getRuleStrength();
			PubDictionary ruleStrengthDt = new PubDictionary();
			ruleStrengthDt.setDicCode(ruleStrength);
			ruleStrengthDt = (PubDictionary) this.pubDictionaryService.selectOne(ruleStrengthDt);

			String regEx = ruleStrengthDt.getExtension1();

			Pattern pattern = Pattern.compile(regEx);
			Matcher matcher = pattern.matcher(password);

			checkMsg.setCheckState(matcher.matches());
			if (!matcher.matches()) {
				checkMsg.setCheckMsg(checkMsg.getCheckMsg() + "������������������������������������������������"
						+ ruleStrengthDt.getDicName() + ";");
			}
			if ((checkExpire) && (PubSecurityRule.getIsSecurityExpire().equals("1"))) {
				if (log != null) {
					createDateStr = log.getEditTime();
				} else {
					PubUser user = (PubUser) this.pubUserService.selectByKey(userId);
					createDateStr = user.getCreateTime();
				}
				Date nowDate = DateUtil.convertToDate(DateUtil.getSystemDateTime(), "yyyy-MM-dd HH:mm:ss");
				if (StringUtil.emptyAndNull(createDateStr)) {
					checkMsg.setExpire(false);
				} else {
					Date createDate = DateUtil.convertToDate(createDateStr, "yyyy-MM-dd HH:mm:ss");
					int day = DateUtil.getDaysBetween(createDate, nowDate);
					if (day <= PubSecurityRule.getPeriod().intValue() - 5) {
						checkMsg.setExpire(false);
					} else {
						checkMsg.setExpire(true);
						checkMsg.setCheckMsg(checkMsg.getCheckMsg() + "��������������������;");
					}
				}
			} else {
				checkMsg.setExpire(false);
			}
		} else {
			checkMsg.setCheckMsg(checkMsg.getCheckMsg() + "����������������������;");
		}
		return checkMsg;
	}

	public void updateSecurityEditLog(String userId, String type) {
		PubSecurityEditLog editLog = new PubSecurityEditLog();
		editLog.setUserId(userId);
		editLog.setSecurityType(type);
		editLog = (PubSecurityEditLog) this.securityEditLogDao.selectOne(editLog);
		if (editLog == null) {
			editLog = new PubSecurityEditLog();
			editLog.setUserId(userId);
			editLog.setSecurityType(type);
			editLog.setLogId(SystemUtil.getUUID());
			editLog.setEditorId(userId);
			editLog.setEditTime(DateUtil.getSystemDateTime());
			this.securityEditLogDao.insertSelective(editLog);
		} else {
			editLog.setEditorId(userId);
			editLog.setEditTime(DateUtil.getSystemDateTime());
			this.securityEditLogDao.updateByPrimaryKeySelective(editLog);
		}
	}

	public PubSecurityRule getLoginSecurityRule() {
		PubSecurityRule securityRule = new PubSecurityRule();
		securityRule.setSecurityType("login");
		securityRule.setIsSecurityExpire("1");

		return (PubSecurityRule) selectOne(securityRule);
	}
}
